package com.ferenc.redisson.service;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author <a href="mailto:ferenc.zeng@gmail.com">Ferenc Zeng</a>
 * @since 2022-02-02
 */
@Service
public class ProductInfoService {

    @Autowired
    private RedissonClient redissonClient;

    public boolean decrementProductStore(Long productId, Integer productQuantity) {
        String key = "dec_store_lock_" + productId;
        RLock lock = redissonClient.getLock(key);
        try {
            //加锁 操作很类似Java的ReentrantLock机制
            lock.lock();
//            ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(productId);
//            //如果库存为空
//            if (productInfo.getProductStock() == 0) {
//                return false;
//            }
//            //简单减库存操作 没有重新写其他接口了
//            productInfo.setProductStock(productInfo.getProductStock() - 1);
//            productInfoMapper.updateByPrimaryKey(productInfo);

            System.out.println("Execute you command!");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            //解锁
            lock.unlock();
        }
        return true;
    }

}
